/*
 * 
 * DILARANG MENGHAPUS ATAU MENGEDIT COPYRIGHT INI.
 * 
 * Copyright 2008 echo.khannedy@gmail.com. 
 * All rights reserved.
 * 
 * Semua isi dalam file ini adalah hak milik dari echo.khannedy@gmail.com
 * Anda tak diperkenankan untuk menggunakan file atau mengubah file
 * ini kecuali anda tidak menghapus atau merubah lisence ini.
 * 
 * File ini dibuat menggunakan :
 * IDE        : NetBeans
 * NoteBook   : Acer Aspire 5920G
 * OS         : Windows Vista
 * Java       : Java 1.6
 * 
 */
package edu.unindra.raport.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import edu.unindra.raport.dao.entity.Operator;

/**
 * 
 * @author usu
 */
public class TabelModelOperator extends AbstractTableModel {

	private static final long serialVersionUID = -1;
	private Vector<Operator> row;
	private Vector<String> column;

	/**
	 * tabel model untuk operator
	 */
	public TabelModelOperator() {
		column = new Vector<String>();
		column.add("Id Operator");
		column.add("Password Operator");
		column.add("Nama Operator");
		column.add("Kontak Operator");
		column.add("Alamat Operator");

		row = new Vector<Operator>();
	}

	public synchronized Operator get(int index) {
		return row.get(index);
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public Operator getOperator(int index) {
		return row.get(index);
	}

	public void add(List<Operator> list) {
		for (Operator o : list) {
			row.add(o);
		}
		fireTableDataChanged();
	}

	/**
	 * remove all row
	 */
	public void removeAllElements() {
		row.removeAllElements();
		fireTableDataChanged();
	}

	/**
	 * 
	 * @param o
	 */
	public void addOperator(Operator o) {
		row.add(o);
		fireTableDataChanged();
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public Operator removeOperator(int index) {
		Operator b = row.remove(index);
		fireTableRowsDeleted(index, index);
		return b;
	}

	/**
	 * 
	 * @param obj
	 * @param index
	 */
	public void setOperatorAt(Operator obj, int index) {
		row.setElementAt(obj, index);
		fireTableRowsUpdated(index, index);
	}

	public int getRowCount() {
		return row.size();
	}

	public int getColumnCount() {
		return column.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.column.get(column);
	}

	/**
	 * 
	 * @param rowIndex
	 * @param columnIndex
	 * @param o
	 */
	public void setValueAt(int rowIndex, int columnIndex, Object o) {
		try {
			if (columnIndex == 0) {
				row.get(rowIndex).setId(o.toString());
			} else if (columnIndex == 1) {
				row.get(rowIndex).setPassword(o.toString());
			} else if (columnIndex == 2) {
				row.get(rowIndex).setName(o.toString());
			} else if (columnIndex == 3) {
				row.get(rowIndex).setContact(o.toString());
			} else if (columnIndex == 4) {
				row.get(rowIndex).setAddress(o.toString());
			}
		} finally {
			fireTableDataChanged();
		}
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return row.get(rowIndex).getId();
		} else if (columnIndex == 1) {
			return row.get(rowIndex).getPassword();
		} else if (columnIndex == 2) {
			return row.get(rowIndex).getName();
		} else if (columnIndex == 3) {
			return row.get(rowIndex).getContact();
		} else if (columnIndex == 4) {
			return row.get(rowIndex).getAddress();
		} else {
			return null;
		}
	}

}
